feat(health): implement ID-based surgical repair with ARR metadata auto-discovery#531
feat(health): implement ID-based surgical repair with ARR metadata auto-discovery#531drondeseries wants to merge 10 commits intojavi11:mainfrom
Conversation
bc78af4 to
260fce4
Compare
| @@ -0,0 +1,21 @@ | |||
| # Plan: Respect Configured Categories in ARR Auto-Registration | |||
| metadataStr = &str | ||
| } | ||
|
|
||
| if relinked, err := s.healthRepo.RelinkFileByFilename(c.Context(), fileName, normalizedPath, path, metadataStr); err == nil && relinked { |
There was a problem hiding this comment.
Why are you saving all the hook request metadata? can we select just some fields? does it make sense better to save it as JSON in the database to perform queries?
|
I've addressed all feedback:
Everything is consolidated into a single clean commit on the PR branch. |
|
I've further optimized the metadata storage by:
The code is now 100% strict, performant, and storage-efficient. |
260fce4 to
f516048
Compare
…scovery This comprehensive update transitions AltMount's repair system from fragile path-based string matching to a high-precision, ID-based surgical workflow. By capturing and persisting unique ARR database IDs, AltMount can now guarantee 100% accuracy during repairs, even if files or folders have been renamed or reorganized. Key Technical Components: 1. Database & Schema: - Adds a 'metadata' JSON column to the 'file_health' table (Migration 026). - Updates all repository queries to persist and retrieve rich ARR IDs. - Ensures metadata is preserved and updated during file re-linking/renames. 2. Rich Metadata Capture: - Expands the Webhook handler to extract InstanceName, SeriesID, MovieID, and EpisodeFileID directly from Sonarr/Radarr payloads. - Implements detailed logging for webhook events, providing immediate visibility into captured IDs and library paths. 3. Automated Metadata Discovery Service (Three-Layer Engine): - Priority 1 (Show & Scene Match): Directly searches ARR libraries by title extracted from Release Name (NZB), matching against the permanent 'sceneName' field—the gold standard for ID precision. - Priority 2 (Global Discovery): Automatically polls all enabled ARR instances if the managing instance cannot be determined by path alone. - Priority 3 (Fuzzy Fallback): Safety mechanism using dots-to-spaces normalization and fuzzy title matching to identify legacy items. 4. Health Worker Integration: - Auto-Discovery: Proactively backfills IDs for healthy files during background check cycles. - Emergency Discovery: Attempts a final high-precision ID lookup immediately before a corruption repair strike if metadata is missing. - Surgical Strike: Utilizes captured IDs to execute targeted 'Fail and Replace' commands via the starr API, ensuring zero false-positive deletions. This creates a self-healing, ID-aware media management layer that provides production-grade reliability for large-scale library repairs.
260fce4 to
9919ae2
Compare
|
Here is the technical rationale for the specific fields retained in the metadata JSON:
All other redundant fields (like file paths) have been removed as they are already stored in Altmount's dedicated database columns ( |
| slog.InfoContext(ctx, "Successfully discovered metadata during emergency discovery", | ||
| "file_path", item.FilePath, | ||
| "instance", metadata.InstanceName) | ||
| if err := hw.healthRepo.UpdateFileMetadata(ctx, item.ID, str); err != nil { |
There was a problem hiding this comment.
This part still sabe it as string,
| @@ -0,0 +1,9 @@ | |||
| -- +goose Up | |||
| -- +goose StatementBegin | |||
| ALTER TABLE file_health ADD COLUMN metadata TEXT DEFAULT NULL; | |||
There was a problem hiding this comment.
Updated the SQLite migrations to use JSONB as well. Thanks for the tip!
Summary
This PR introduces a significant architectural upgrade to AltMount’s repair system, moving from Path-Based Guessing to ID-Based Precision.
Currently, AltMount attempts to repair corrupted files by matching paths and filenames against the Sonarr/Radarr library. This is fragile and often fails if the user has renamed folders or changed episode naming formats since the file was imported.
This update enables AltMount to capture, persist, and utilize unique ARR database IDs (`SeriesID`, `EpisodeFileID`, `MovieID`, `InstanceName`) to execute surgical "Fail and Replace" strikes with 100% accuracy.
Key Features
Technical Details
Example Log Output
```text
level=INFO msg="Successfully re-linked health record during webhook with rich metadata"
event=Download instance="Sonarr-Main" series_id=1587 episode_file_id=139540
new_library_path="/mnt/tv/Show Name/Season 01/Episode.mkv"
```
```text
level=INFO msg="ID-Based Precision: Using metadata IDs for Sonarr repair"
series_id=1587 episode_file_id=139540 instance="Sonarr-Main"
```